package com.demo.handler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import com.demo.bean.ResponseResult;
import com.demo.exception.FileUploadException;

/**
 * 全局异常处理器
 * @author Administrator
 *
 */
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

	@ExceptionHandler(MultipartException.class)
	public ResponseResult m1(MultipartException multipartException) {
		String message = "文件上传异常";
		if (multipartException instanceof MaxUploadSizeExceededException) {
			message = "单文件大小超过限制";
		}
		return new ResponseResult(500, message);
	}

	@ExceptionHandler(FileUploadException.class)
	public ResponseResult m1(FileUploadException fileUploadException) {
		//String message = "自定义文件上传异常";

		return new ResponseResult(fileUploadException.getCode(), fileUploadException.getMessage());
	}

}
